$(document).ready(function(){  
   $(".importresumesdbtn").click(function(){
      //关闭dropdown的展开
      $("div.dropdown").removeClass("open");
      var target = $(this);
      var endid = $(this).data("endid");
      if($("#batchimport_dialog"+endid).length <=0) {
         $.get(target.attr('href'), function(html) {
            $('body').append(html);
            var s1 = target.offset().left;
            var s2 = target.offset().top - $(window).scrollTop();
            $("#batchimport_dialog"+endid).show().css({
               top:s2+40+"px",
               left:s1-320+"px"
            });

            // init the upload form 
            $('#batchimport_dialog'+endid+' #uploadresumeform').fileUploadUIX({
               mixedFileTypes: ['docTypes', 'compressTypes'],
               maxFileSize: 10*1024*1024,
               keepList: false,
               autoUpload: false,
               onSuccess: function(uix, handler, options) {
                  $('#batchimport_dialog'+endid+' .notice').fadeIn(); 
                  handler.uploadRow.find('.file_upload_progress').html('').append($('<span class="success"/>').append("已加入任务列表"));
                  setTimeout(function() { handler.uploadRow.fadeOut(); }, 3000);
               }, 
            }); 

            // init third-party import 
            $('#batchimport_dialog'+endid).on('click', '.importresume', function() {
               var target = $(this);
               var data = {
                  type: target.data("type"),
                  syncaction: target.data("syncaction")
               };

               startSync(target.data("type"), data,function(submitted){
                  if(typeof(submitted)!='undefined' && submitted)
                     target.removeClass('importresume').addClass('synchronous_ing').attr('title','简历导入中，请稍后').html('导入中...');
               });
               return false; 
            });
         });
      }
      else {
         $("#batchimport_dialog"+endid).show().position({
            my:"right top+10",
            at:"right bottom",
            of:target
         });
      }

      var mhandler = setInterval(function() {
         if($("#batchimport_dialog"+endid).css('display') == 'none') {
            clearInterval(mhandler); 
            return false; 
         }

         var any = getMaxZIndex(".modal");
         var my = getMaxZIndex("#batchimport_dialog"+endid); 
         any > my ? $("#batchimport_dialog"+endid).addClass("keepopen") : $("#batchimport_dialog"+endid).removeClass("keepopen");
      }, 200);

      return false;
   });

   $(document).bind('keydown', function(event) {
      if(event.keyCode == 27) {
         $('.popup').not('.keepopen').hide().closest('.dropdown.open').removeClass('open');
         return true;
      }
   });
   
   $(document).bind('mousedown', function(e){
      e = $.event.fix(e);
      var otarget = $(e.target);
      if(otarget.closest(".popup").length>0) {
         if(otarget.hasClass("closediv") && !otarget.hasClass("resumeshare")) {
            otarget.closest(".popup").hide();
            return true;
         }
         return true;
      }
      // close all popup that no need keep and dropdown by open class
      $('.popup').not('.keepopen').hide().closest('.dropdown.open').removeClass('open');
      return true;
   })
   
   $(document).on('click','#uploadresumetable td .btn.small', function(){
      $(this).closest('.file_upload_template').remove();
   })

});